package indi.xliuovo.coursedesign.musicplayer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with Android Studio
 *
 * @Author xliuOvO
 */
public class LoginFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    View loginFragment;
    EditText userId, userPassword;
    Button btnLogin;
    TextView btnForget;

    private String mParam1;
    private String mParam2;

    public LoginFragment() {
    }

    public static LoginFragment newInstance(String param1, String param2) {
        LoginFragment fragment = new LoginFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        loginFragment = inflater.inflate(R.layout.fragment_login, container, false);
        return loginFragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initWidget();
        btnLogin.setOnClickListener(v -> {
            if (!isEmpty()) {
                // 传递数据
                Map<String, String> loginUser = new HashMap<String, String>();
                loginUser.put("userId", userId.getText().toString());
                loginUser.put("password", userPassword.getText().toString());
                MyThread myThread = new MyThread();
                myThread.toParams("user/login", loginUser); // 处理数据
                myThread.start();
                try {
                    myThread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (((Double) myThread.result.get("code")).intValue() == 400) { // 处理登录失败
                    Toast.makeText(getContext(), String.valueOf(myThread.result.get("msg")), Toast.LENGTH_SHORT).show();
                    return;
                } else if (((Double) myThread.result.get("code")).intValue() == 200) { // 处理登录成功
                    Bundle bundle = new Bundle();
                    bundle.putString("userId", userId.getText().toString());
                    Intent intent = new Intent(getContext(), MainActivity.class);
                    intent.putExtras(bundle); // 传递数据
                    startActivity(intent);
                    getActivity().finish();
                }
            }
        });
        btnForget.setOnClickListener(v -> {
            ForgetPwdFragment forgetPwdFragment = new ForgetPwdFragment();
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, forgetPwdFragment).commit();
        });
    }

    // 初始化控件
    void initWidget() {
        userId = loginFragment.findViewById(R.id.userId);
        userPassword = loginFragment.findViewById(R.id.userPassword);
        btnLogin = loginFragment.findViewById(R.id.toLogin);
        btnForget = loginFragment.findViewById(R.id.forget);
    }

    // 判断输入项是否有空
    boolean isEmpty() {
        if (TextUtils.isEmpty(userId.getText()) || TextUtils.isEmpty(userPassword.getText().toString())) {
            Toast.makeText(getContext(), "有输入项为空", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
}